/**
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import java.awt.Component;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.datatransfer.TransferableFactory;

public class ResponsePrintViewController extends edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel implements edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement {

 protected edu.cmu.cs.stage3.alice.core.response.Print printStatement;
 protected javax.swing.JPanel subPanel = new edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel();
 protected javax.swing.JLabel printPrefixLabel = new javax.swing.JLabel( "Print: " );
 protected javax.swing.JLabel printSuffixLabel = new javax.swing.JLabel( "" );
 protected java.util.HashMap guiMap = new java.util.HashMap();
 protected MouseListener mouseListener = new MouseListener();
 protected edu.cmu.cs.stage3.alice.core.event.PropertyListener commentedListener = new edu.cmu.cs.stage3.alice.core.event.PropertyListener() {
  public void propertyChanging( edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev) {}
  public void propertyChanged( edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev) {
   if (ev.getValue().equals( Boolean.TRUE )) {
    setEnabled( false );
   } else {
    setEnabled( true );
    }
   ResponsePrintViewController.this.revalidate();
   ResponsePrintViewController.this.repaint();
   }
  };
 protected edu.cmu.cs.stage3.alice.core.event.PropertyListener updateListener = new edu.cmu.cs.stage3.alice.core.event.PropertyListener() {
  public void propertyChanging (edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev) {}
  public void propertyChanged (edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev) {
   ResponsePrintViewController.this.refreshGUI();
   }
  };
 protected boolean sleeping = false;

 public ResponsePrintViewController () {
  setBorder( javax.swing.BorderFactory.createEmptyBorder( 1, 2, 3, 2 ) );

  subPanel.setLayout( new java.awt.GridBagLayout() );
  subPanel.setOpaque( false );
  subPanel.setBorder( null );

  addMouseListener( mouseListener );
  grip.addMouseListener( mouseListener );
  subPanel.addMouseListener( mouseListener ); // this didn't used to be necessary. I don't know what's going on
  }

 public edu.cmu.cs.stage3.alice.core.response.Print get () {
  return printStatement;
  }

 public void set (edu.cmu.cs.stage3.alice.core.response.Print printStatement) {
  super.reset();

  stopListening();

  this.printStatement = printStatement;
  if (this.printStatement != null) {
   setTransferable( TransferableFactory.createTransferable( printStatement ) );
   setBackground( AuthoringToolResources.getColor( "Print" ) );

   this.add( subPanel, java.awt.BorderLayout.CENTER );
   this.addDragSourceComponent( subPanel );

   startListening();
   }

  refreshGUI();
  }

 protected void startListening () {
  if (printStatement != null) {
   printStatement.text.addPropertyListener( updateListener );
   printStatement.object.addPropertyListener( updateListener );
   }
  }

 protected void stopListening () {
  if (printStatement != null) {
   printStatement.text.removePropertyListener( updateListener );
   printStatement.object.removePropertyListener( updateListener );
   }
  }

 //Rem @Override
 public void setEnabled (boolean b) {
  super.setEnabled( b );
  java.awt.Component[] children = subPanel.getComponents();
  //LOB for (Component element : children)
  int t_tam = children!=null ? children.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   children[i_i].setEnabled( b ); //L element.setEnabled( b );
   }
  }

 public void goToSleep () {
  stopListening();
  sleeping = true;
  }

 public void wakeUp () {
  startListening();
  sleeping = false;
  }

 public void clean () {
  stopListening();
  printStatement = null;
  setTransferable( null );
  removeAll();
  }

 public void die () {
  clean();
  }

 //Rem @Override
 public void release () {
  super.release();
  edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
  }

 public void refreshGUI () {
  subPanel.removeAll();

  if (printStatement != null) {
   String format = AuthoringToolResources.getFormat( printStatement.getClass() );
   edu.cmu.cs.stage3.alice.authoringtool.util.FormatTokenizer formatTokenizer = new edu.cmu.cs.stage3.alice.authoringtool.util.FormatTokenizer( format );
   if (formatTokenizer.hasMoreTokens()) {
    printPrefixLabel.setText( formatTokenizer.nextToken() );
   } else {
    printPrefixLabel.setText( "" );
    }
   String token = null;
   while( formatTokenizer.hasMoreTokens()) {
    token = formatTokenizer.nextToken();
   }
   if ((token != null) && (! (token.startsWith( "<" ) && token.endsWith( ">" )))) {
    printSuffixLabel.setText( token );
   } else {
    printSuffixLabel.setText( "" );
    }

   int i = 0;
   if (printStatement.text.get() == null) {
    boolean omitName = AuthoringToolResources.shouldGUIOmitPropertyName( printStatement.object );
    javax.swing.JComponent objectPropertyGui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyViewController( printStatement.object, true, true, omitName, new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( printStatement.object ) );

    subPanel.add( printPrefixLabel, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    subPanel.add( objectPropertyGui, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    subPanel.add( printSuffixLabel, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
   } else if (printStatement.object.get() == null) {
    boolean omitName = AuthoringToolResources.shouldGUIOmitPropertyName( printStatement.text );
    javax.swing.JComponent textPropertyGui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyViewController( printStatement.text, true, true, omitName, new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( printStatement.text ) );

    subPanel.add( printPrefixLabel, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    subPanel.add( textPropertyGui, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    subPanel.add( printSuffixLabel, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
   } else {
    boolean omitName = AuthoringToolResources.shouldGUIOmitPropertyName( printStatement.text );
    javax.swing.JComponent textPropertyGui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyViewController( printStatement.text, true, true, omitName, new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( printStatement.text ) );
    omitName = AuthoringToolResources.shouldGUIOmitPropertyName( printStatement.text );
    javax.swing.JComponent objectPropertyGui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyViewController( printStatement.object, true, true, omitName, new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( printStatement.object ) );

    subPanel.add( printPrefixLabel, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    subPanel.add( textPropertyGui, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 2 ), 0, 0 ) );
    subPanel.add( objectPropertyGui, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    subPanel.add( printSuffixLabel, new java.awt.GridBagConstraints( i++, 0, 1, 1, 0.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
    }

   subPanel.add( javax.swing.Box.createGlue(), new java.awt.GridBagConstraints( i++, 0, 1, 1, 1.0, 0.0, java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.HORIZONTAL, new java.awt.Insets( 0, 0, 0, 0 ), 0, 0 ) );
   }

  revalidate();
  repaint();
  }

 class MouseListener extends edu.cmu.cs.stage3.alice.authoringtool.util.CustomMouseAdapter {
  //Rem @Override
  public void popupResponse (java.awt.event.MouseEvent ev) {
   java.util.Vector structure = edu.cmu.cs.stage3.alice.authoringtool.util.ElementPopupUtilities.getDefaultStructure( ResponsePrintViewController.this.printStatement );
   if ((structure != null) && (! structure.isEmpty())) {
    // call action: edu.cmu.cs.stage3.alice.authoringtool.util.ElementPopupUtilities.createAndShowElementPopupMenu(...)
    edu.cmu.cs.stage3.alice.authoringtool.util.ElementPopupUtilities.createAndShowElementPopupMenu( ResponsePrintViewController.this.printStatement, structure, ResponsePrintViewController.this, ev.getX(), ev.getY() );
    }
   }
  }

 }
